package com.zc.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.edu.entity.Course;
import com.zc.edu.entity.CourseDescription;
import com.zc.edu.entity.vo.CourseInfoVo;
import com.zc.edu.entity.vo.CoursePublishVo;
import com.zc.edu.entity.vo.front.CourseFrontVo;
import com.zc.edu.mapper.CourseMapper;
import com.zc.edu.service.ChapterService;
import com.zc.edu.service.CourseDescriptionService;
import com.zc.edu.service.CourseService;
import com.zc.edu.service.VideoService;
import com.zc.servicebase.exception.CongException;
import com.zc.utils.R;
import com.zc.utils.vo.CourseShowInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author cong
 * @since 2022-11-25
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

	@Autowired
	private CourseDescriptionService courseDescriptionService;

	@Autowired
	private VideoService videoService;

	@Autowired
	private ChapterService chapterService;

	@Override
	public String addCourseInfo(CourseInfoVo courseInfoVo) {
		// 向课程表中加数据
		Course course = new Course();
		BeanUtils.copyProperties(courseInfoVo, course);
		boolean isSave = this.save(course);
		if (!isSave) {
			throw new CongException(20001, "添加课程失败");
		}

		String courseId = course.getId();
		// 向课程描述表中添加数据
		CourseDescription courseDescription = new CourseDescription();
		BeanUtils.copyProperties(courseInfoVo, courseDescription);
		courseDescription.setId(courseId);
		courseDescriptionService.save(courseDescription);
		return courseId;
	}

	@Override
	public CourseInfoVo getCourseInfo(String courseId) {
		// 查询课程表信息
		Course courseInfo = this.getById(courseId);
		CourseInfoVo courseInfoVo = new CourseInfoVo();
		if (courseInfo != null) {
			BeanUtils.copyProperties(courseInfo, courseInfoVo);
		}


		// 查询课程描述表
		CourseDescription courseDescription = courseDescriptionService.getById(courseId);
		//BeanUtils.copyProperties(courseDescription, courseInfoVo);
		if (courseInfo != null) {
			courseInfoVo.setDescription(courseDescription.getDescription());
		}
		return courseInfoVo;
	}

	@Override
	public boolean updateCourseInfo(CourseInfoVo courseInfoVo) {
		// 更新课程表信息
		Course course = new Course();
		BeanUtils.copyProperties(courseInfoVo, course);
		boolean result = this.updateById(course);

		// 更新课程描述信息
		if (result) {
			CourseDescription courseDescription = new CourseDescription();
			BeanUtils.copyProperties(courseInfoVo,courseDescription);
			result = courseDescriptionService.updateById(courseDescription);
		} else {
			throw new CongException(R.error().getCode(), "修改课程信息失败");
		}
		return result;
	}

	@Override
	public CoursePublishVo getCoursePublishVo(String courseId) {
		CoursePublishVo coursePublishVo = baseMapper.getCoursePublishVo(courseId);
		return coursePublishVo;
	}

	@Override
	public boolean deleteCourse(String courseId) {
		videoService.removeVideo(courseId);
		chapterService.removeChapter(courseId);
		courseDescriptionService.removeById(courseId);
		boolean remove = this.removeById(courseId);

		return remove;
	}

	@Override
	public Map<String, Object> getCourseList(Page<Course> pageCourse, CourseFrontVo courseFrontVo) {
		QueryWrapper<Course> wrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
			wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
		}

		if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
			wrapper.eq("subject_id", courseFrontVo.getSubjectId());
		}

		if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
			wrapper.orderByDesc("buy_count");
		}

		if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
			wrapper.orderByDesc("gmt_create");
		}

		if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
			wrapper.orderByDesc("price");
		}

		this.page(pageCourse, wrapper);
		List<Course> records = pageCourse.getRecords();
		long total = pageCourse.getTotal();
		long size = pageCourse.getSize();
		long current = pageCourse.getCurrent();
		long pages = pageCourse.getPages();
		boolean hasNext = pageCourse.hasNext();
		boolean hasPrevious = pageCourse.hasPrevious();
		HashMap<String, Object> pageInfo = new HashMap<>();
		pageInfo.put("records", records);
		pageInfo.put("current", current);
		pageInfo.put("pages", pages);
		pageInfo.put("size", size);
		pageInfo.put("total", total);
		pageInfo.put("hasNext", hasNext);
		pageInfo.put("hasPrevious", hasPrevious);
		return pageInfo;
	}

	@Override
	public CourseShowInfo getFrontCourseInfo(String id) {
		CourseShowInfo courseShowInfo = baseMapper.getCourseShowInfo(id);
		return courseShowInfo;
	}
}
